#include "win32/throw_win32_exception.h"
#include "win32/win32_exception.h"
#include "win32/lean_windows.h"

#include <stdio.h>

void Throw_Win32_Exception(const char* function_name)
{
	char szBuf[512] = { 0 };
	char* lpMsgBuf;
	DWORD dw = GetLastError(); 

	if (dw == ERROR_SUCCESS)
		return;

	FormatMessageA(
		FORMAT_MESSAGE_ALLOCATE_BUFFER |
		FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		dw,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(char*) &lpMsgBuf,
		0, NULL );

	_snprintf(
		szBuf,
		511,
		"%s failed with error %d: %s", 
		function_name, dw, lpMsgBuf);

	throw Exception(szBuf);

}